package com.edu.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.edu.demo.entity.UserBean;
import com.edu.demo.entity.dto.UserLoginDTO;
import com.edu.demo.entity.vo.UserLoginByPassVO;
import com.edu.demo.result.Result;
import com.edu.demo.service.IUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author XYH
 * date 2024-03-08
 */
//@Controller + @ResponseBody = @RestController
@Api(tags = "登录接口")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private IUserLoginService iUserLoginService;


    @GetMapping("/hello")
    public Result getMessage() {
        //默认返回页面地址 @ResponseBody 返回才是内容
        return new Result();
    }

    @ApiOperation(value = "用户名密码登录")
    @PostMapping("/bypass")
    public Result<UserLoginDTO> userLoginByPass(@Valid @RequestBody UserLoginByPassVO userLoginByPassVO) {
        UserBean userBean = new UserBean();
        BeanUtil.copyProperties(userLoginByPassVO, userBean);
        UserLoginDTO userLoginDTO = iUserLoginService.userLoginByPass(userBean);
        if (userLoginDTO == null) {
            return Result.error(1001, "用户名或密码错误");
        }
        return Result.success(userLoginDTO);
    }

}
